package com.qdlc.p2p.common.util;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * Created by lenovo on 2016/1/4.
 */
public class MessageCheck {

    private static Map<String, Object> validateCodeMap;

    private final static int max = 200;

    static{
        validateCodeMap = Maps.newConcurrentMap();
    }

    /**
     *添加手机号
     */
    public void mobileAdd(String mobile){
        validateCodeMap.put(mobile, DateUtil.getNowTime()+60);
    }

    /**
     * 距离上次短信发送的时间差
     */

    public int mobileCheck(String mobile){
        String time="0";
        if(validateCodeMap.get(mobile)!=null)
            time=validateCodeMap.get(mobile)+"";
        int timeCheck=Integer.parseInt(time)-DateUtil.getNowTime();
        if(timeCheck>0)
            return timeCheck;
        else
            return 0;
    }

    /**
     * 删除指定的过期的键值对
     */
    public void removeMobile(String mobile){
        validateCodeMap.remove(mobile);
    }

    /**
     * 删除所有的过期的键值对
     */
    public void removeAll(){
        if(validateCodeMap.size()>max) {
            int nowTime = DateUtil.getNowTime();
            for (Map.Entry<String, Object> entry : validateCodeMap.entrySet()) {
                if (nowTime > Integer.valueOf(entry.getValue() + ""))
                    validateCodeMap.remove(entry.getKey());
            }
        }
    }
}
